package mdm.led.service.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import mdm.led.dao.RoleMapper;
import mdm.led.entity.Permission;
import mdm.led.entity.Role;
import mdm.led.entity.RolePermission;
import mdm.led.service.IRoleService;
import mdm.led.util.DateUtil;

@Service("iroleService")
public class IRoleServiceImpl implements IRoleService {

    
    @Autowired
    private RoleMapper roleMapper;
    
    
    @Override
    public Role getByRoleId(int roleid) {
        return roleMapper.getByRoleId(roleid);
    }

    @Override
    public List<Role> selectByCondition(Role role) {
        return roleMapper.selectByCondition(role);
    }

    @Override
    public List<Permission> selectByPermission() {
        return roleMapper.selectByPermission();
    }

    @Override
    public void insertSelective(Role role) {
        short state = 1;
        role.setState(state);
        try {
            role.setCreateTime(DateUtil.formatDateTime(new Date()));
        } catch (Exception e) {
            e.printStackTrace();
        }
        roleMapper.insertSelective(role);
//        for(RolePermission p :role.getRolepermissionlist()) {
//            p.setcRoleId(role.getcRoleId());
//            roleMapper.insertRolePermissionSelective(p);
//        }
        if(role.getRolepermissionlist()!=null) {
       	 for(RolePermission p :role.getRolepermissionlist()) {
            	 RolePermission rolePermission = new RolePermission();
            	 rolePermission.setcRoleId(role.getcRoleId());
            	 rolePermission.setcPermissionDetailId(p.getcPermissionDetailId());
            	 roleMapper.insertRolePermissionSelective(rolePermission);
            }
       }
    }

    @Override
    public void deleteRole(int roleid) {
        short state = 2;
        Role role = new Role();
        role.setcRoleId(roleid);
        role.setState(state);
        roleMapper.updateRole(role);
    }

    @Override
    public void Enabledisable(Role role) {
        roleMapper.updateRole(role);
    }
    
    @Override
    public void updateRole(Role role) {
        try {
            role.setUpdateTime(DateUtil.formatDate(new Date()));
            roleMapper.updateRole(role);
        } catch (Exception e) {
            e.printStackTrace();
        }
        
        if(role.getRolepermissionlist()!=null) {
        	roleMapper.deleteByRolePermissionId(role.getcRoleId());
        	for(RolePermission p :role.getRolepermissionlist()) {
            	 RolePermission rolePermission = new RolePermission();
            	 rolePermission.setcRoleId(role.getcRoleId());
            	 rolePermission.setcPermissionDetailId(p.getcPermissionDetailId());
                 roleMapper.insertRolePermissionSelective(rolePermission);
            }
       }
        
//        Role r = roleMapper.getByRoleId(role.getcRoleId());
//        List<Integer> lodPermissionlist = toPermissionId(r.getRolepermissionlist());
//        List<Integer> newPermissionlist = toPermissionId(role.getRolepermissionlist());
//        List<Integer> delp = toDelPermission(lodPermissionlist,newPermissionlist);
//        List<Integer> addp = toAddPermission(lodPermissionlist,newPermissionlist);
//        
//        for(Integer del : delp) {
//            RolePermission rp =new RolePermission();
//            rp.setcRoleId(role.getcRoleId());
//            rp.setcPermissionId(del);
//            roleMapper.deleteByRolePermission(rp);
//        }
//        
//        for(Integer add : addp) {
//            RolePermission rp =new RolePermission();
//            rp.setcRoleId(role.getcRoleId());
//            rp.setcPermissionId(add);
//            roleMapper.insertRolePermissionSelective(rp);
//        }
        
    }

    /**
     * 获取需要删除的权限
     * @param existList   已有的权限
     * @param alllist     新的权限
     * @return
     */
    public List<Integer> toDelPermission(List<Integer> existList, List<Integer> alllist) {
        List<Integer> list = new ArrayList<Integer>();
        for (Integer rp : existList) {
            // 如果系统原有的不存在与APP端所有的
            if (!alllist.contains(rp)) {
                list.add(rp);
            }
        }
        return list;
    }
    
    /**
     * 获取需要添加的权限
     * @param existList   已有的权限
     * @param alllist     新的权限
     * @return
     */
    public List<Integer> toAddPermission(List<Integer> existList, List<Integer> alllist) {
        List<Integer> list = new ArrayList<Integer>();
        for (Integer rp : alllist) {
            if (!existList.contains(rp)) {
                list.add(rp);
            }
        }
        return list;
    }
    
    public List<Integer> toPermissionId(List<RolePermission> list){
        List<Integer> perminsslist = new ArrayList<Integer>();
        
        for(RolePermission r : list) {
            perminsslist.add(r.getcPermissionDetailId());
        }
        return perminsslist;
    }

	@Override
	public Role selectByName(Role role) {
		return roleMapper.selectByName(role);
	}

	/**
     * 查询某个角色下的权限
     * @param role
     * @return
     */
	@Override
	public List<Permission> initPermissonList(Role role) {
		return roleMapper.findPermitionByRole(role);
	}

	@Override
	public List<RolePermission> selectByRoleIdList(int roleid) {
		// TODO Auto-generated method stub
		return roleMapper.selectByRoleIdList(roleid);
	}

	@Override
	public void deleteByRolePermissionId(int roleid) {
		roleMapper.deleteByRolePermissionId(roleid);
	}
    
}
